package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //新增套餐
        setmealMapper.saveSetmeal(setmeal);

        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            log.info(setmealDishes.toString());
            //向套餐菜单表插入n条数据
            setmealDishMapper.inserBatch(setmealDishes);
        }
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐删除
     * @param ids
     */
    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void delete(List<Long> ids) {
        //查询套餐是否在售
        Integer count= setmealMapper.getCountByIdAndStatus(ids, StatusConstant.ENABLE);
        if (count>0){
            throw new BaseException("套餐在售，不能删除");
        }
        //删除套餐关系表
        setmealDishMapper.deleteBySetmealId(ids);
        //删除套餐
        setmealMapper.deleteByIds(ids);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.getSetmealById(id);
        setmealVO.setSetmealDishes(setmealDishMapper.getSetmealDishById(id));
        return setmealVO;
    }

    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //修改套餐
        setmealMapper.updateSetmeal(setmeal);

        Long setmealId = setmeal.getId();
        List<Long> setmealIds = new ArrayList<>();
        setmealIds.add(setmealId);
        //删除套餐关系表
        setmealDishMapper.deleteBySetmealId(setmealIds);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            log.info(setmealDishes.toString());
            //向套餐菜单表插入n条数据
            setmealDishMapper.inserBatch(setmealDishes);
        }
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @Override
    public void startSealOrNot(Integer status, Long id) {
        setmealMapper.startSealOrNot(status,id);
    }
}
